AddPrefabPostInit('dragonfly',function (inst)
    if not TheWorld.ismastersim then
        return inst
    end
    if inst.components.lootdropper then
        local old_lootsetupfn = inst.components.lootdropper.lootsetupfn
        inst.components.lootdropper:SetLootSetupFn(function (...)
            if math.random() <= TUNING.MOD_SURTR.FLAME_ASH.DRAGONFLY_DROPCHANCE then
                S_SURTR:flingItem(SpawnPrefab('surtr_flame'),inst:GetPosition())
            end
            return old_lootsetupfn ~= nil and old_lootsetupfn(...)
        end)
    end
end)

AddPrefabPostInit('klaus_sack',function(inst)
    if not TheWorld.ismastersim then
        return inst
    end
    if inst.components.klaussacklock then
        local old_UseKey = inst.components.klaussacklock.UseKey
        function inst.components.klaussacklock:UseKey(...)
            local res = old_UseKey ~= nil and {old_UseKey(self, ...)} or {}
            if res and res[1] then
                if math.random() < TUNING.MOD_SURTR.FLAME_ASH.KLAUS_BAG then
                    S_SURTR:flingItem(SpawnPrefab('surtr_flame'),inst:GetPosition())
                end
            end
            return unpack(res)
        end
    end
end)

AddPrefabPostInit('minotaurchest',function (inst)
    if not TheWorld.ismastersim then
        return inst
    end
    local function onopen_1734066012(inst,data)
        if inst.not_open_1734065821 then
            inst.not_open_1734065821 = false
            if inst.components.container then
                inst.components.container:GiveItem(SpawnPrefab('surtr_flame'))
            end
        end
    end

---@diagnostic disable-next-line: inject-field
    inst.not_open_1734065821 = true

    inst:ListenForEvent("onopen",onopen_1734066012)

    S_SURTR:hookFn(inst,'OnSave',function (inst,data)
        data.not_open_1734065821 = inst.not_open_1734065821
    end)
    S_SURTR:hookFn(inst,'OnLoad',function (inst,data)
        inst.not_open_1734065821 = data and data.not_open_1734065821
        if not inst.not_open_1734065821 then
            inst:RemoveEventCallback('onopen',onopen_1734066012)
        end
    end)
end)